package com.wen.center.user.controller;

import com.wen.center.user.controller.model.RegistParams;
import com.wen.center.user.dao.UserInfoDao;
import com.wen.center.user.domain.UserInfo;
import com.wen.center.utils.CoderUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Encoder;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

/**
 * Created by LAIO on 2018/6/3.
 */
@RestController
@RequestMapping("/regist")
public class RegistController {

    private Logger logger = LoggerFactory.getLogger(RegistController.class);

    @Autowired
    private UserInfoDao userInfoDao;

    /**
     * /regist/phone
     * 手机方式注册
     *
     * @param registParams 注册参数
     */
    @RequestMapping(value = "/phone", method = RequestMethod.POST)
    public UserInfo registPhoneUser(@Validated @RequestBody RegistParams registParams) throws NoSuchAlgorithmException {
        logger.info("start regist");
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(registParams, userInfo);
        userInfo.setPassword(CoderUtils.encoderMD5(userInfo.getPassword()));
        userInfo.setCreateTime(new Date());
        userInfo.setUpdateTime(new Date());
        userInfoDao.save(userInfo);
        return userInfo;
    }

    /**
     * /regist/email
     * 邮件方式注册
     *
     * @param registParams 注册参数
     */
    @RequestMapping(value = "/email", method = RequestMethod.POST)
    public UserInfo registEmailUser(@Validated @RequestBody RegistParams registParams) throws NoSuchAlgorithmException {
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(registParams, userInfo);
        userInfo.setPassword(CoderUtils.encoderMD5(userInfo.getPassword()));
        userInfo.setCreateTime(new Date());
        userInfo.setUpdateTime(new Date());
        userInfoDao.save(userInfo);
        return userInfo;
    }
}
